class h_store {

	async getStoreByStoreId(storeId ) {
		return this.resolve(await this._getStoreByStoreId(storeId))
	}

	async getStoreDataByStoreId(storeId) {
		return this.resolve(await this._getStoreDataByStoreId(storeId))
	}

	async getStoreByCodeId(codeId) {
		const code = await this._getCodeData(codeId)
		if(!code)
			return this.reject('-2017', 'gtcode not found')
		if(code.storeId == '') 
			return this.reject('-2018', 'gtcode not activate')
		if(code.codeFreeze)
			return this.reject('-2018', 'gtcode is freeze')
		const {storeId, tableNum} = code
		let store = await this._getStoreByStoreId(storeId)
		store.storeId = storeId
		store.tableNum = tableNum
		return this.resolve(store)
	}

	async getCommentByStoreId(storeId, index = 0, count = 20) {
		const comment = await this._getCommentDataByStoreId(storeId, index, count)
		return this.resolve(comment)
	}

	async getTableNumByCodeId(codeId) {
		const code = await this._getCodeData(codeId)
		if(!code || code.storeId == '' || code.tableNum == '')
			return this.reject('-2018', 'gtcode not activate')
		return this.resolve(code)
	}

	async getPrinters(storeId) {
		const printers = await this.mysql.common.query('select printerId, printerCode, templateContent from store_printer p join printer_template t on p.printerTemplate=t.templateId where p.printerStore=?', [storeId])
		return printers
	}

	async checkAutoOrder(storeId) {
		const result = await this.mysql.common.select('store_order', 'storeAutoOrder', 'storeId=?', [storeId])
		if(result[0] && result[0].storeAutoOrder == 1)
			return true
		return false
	}

	async checkUsePrinter(storeId) {
		const result = await this.mysql.common.select('store_printer', 'printerId', 'printerStore=?', [storeId])
		if(result[0])
			return true
		return false
	}

	async addGoods(storeId, goodsCategory, goodsName, goodsDescription, goodsSpec, goodsChooseSpec = 0) {
		if(!this._checkGoodsCategory(goodsCategory))
			return this.reject('-2064', 'goods category invalid')
		const categorys = await this.mysql.store.select(`${storeId}_category`, 'categoryName', 'categoryId=?', [goodsCategory])
		if(!categorys[0])
			return this.reject('-2065', 'goods category not found')
		const goodsId = await this.common.createRandomStr(6)
		const result = await this.mysql.store.insert(`${storeId}_menu`, 'goodsId, goodsCategory, goodsName, goodsDescription, goodsSpec, goodsChooseSpec', [goodsId, goodsCategory, goodsName, goodsDescription, goodsSpec, goodsChooseSpec])
		if(result == 0)
			return this.reject('-2066', 'add goods failed')
		return goodsId
	}

	async _getStoreByStoreId(storeId) {
		let store = await this._getStoreDataByStoreId(storeId)
		if(!store)
			return this.reject('-2009', 'store not found')
		if(store.storeFreeze)
			return this.reject('-2011', 'store is freeze')
		store.storeIsBusiness = store.storeBusiness != '' && this._checkStoreBusiness(store.storeBusiness)
		const menu = await this._getMenuDataByStoreId(storeId)
		const coupon = await this._getCouponDataByStoreId(storeId)
		return {
			store,
			menu,
			coupon
		}
	}

	async _getCodeData(codeId) {
		if(!codeId || !this._checkCodeId(codeId))
			return this.reject('-2016', 'gtcode invalid')
		const tableIndex = this.common.hashTableIndex(codeId)
		if(!await this.mysql.code.tableExists(`map_${tableIndex}`))
			return null
		const code = await this.mysql.code.select(`map_${tableIndex}`, 'storeId, tableNum, codeFreeze', 'codeId=?', [codeId])
		return code[0] || null
	}

	async _getStoreDataByStoreId(storeId, index, count) {
		if(!this._checkStoreId(storeId))
			return this.reject('-2014', 'storeId invalid')
		const store = await this.mysql.user.select('store_user', 'storeId, storeName, storeDescription, storeAddress, storePhone, storeScore, storeBusiness, storeOrderSum, storeUserSum, storeLat, storeLng, storePayMode, storeFreeze, storeAutoOrder, storeTakeOut', 'storeId=?', [storeId])
		return store[0] || null
	}

	async _getMenuDataByStoreId(storeId) {
		if(!this._checkStoreId(storeId))
			return this.reject('-2014', 'storeId invalid')
		const category = await this.mysql.store.select(`${storeId}_category`, 'categoryId, categoryName', null, 'categoryIndex asc')
		if(!category[0])
			return this.reject('-2013', 'store category not found')
		const goods = await this.mysql.store.select(`${storeId}_menu`, 'goodsId, goodsCategory, goodsName, goodsSpec, goodsChooseSpec, goodsDate, goodsSellOut', 'goodsPut=1 and goodsFreeze=0', null, 'goodsDate desc')
		if(!goods[0])
			return this.reject('-2014', 'store goods not found')
		return {
			category,
			goods
		}
	}

	async _getCouponDataByStoreId(storeId) {
		if(!this._checkStoreId(storeId))
			return this.reject('-2014', 'storeId invalid')
		const coupon = await this.mysql.store.select(`${storeId}_coupon`, 'couponId, couponType, couponName, couponDescription, couponDeduction, couponDiscount, couponLowPay, couponStartDate, couponEndDate, couponCount, couponDate', 'couponFreeze=0')
		return coupon || []
	}

	async _getCommentDataByStoreId(storeId, index, count) {
		if(!this._checkStoreId(storeId))
			return this.reject('-2014', 'storeId invalid')
		let comment
		if(count == 0)
			comment = await this.mysql.comment.select(storeId, 'commentId, commentUserType, commentUser, commentUserName, commentContent, commentPhotos, commentStar, commentDate')
		else 
			comment = await this.mysql.comment.select(storeId, 'commentId, commentUserType, commentUser, commentUserName, commentContent, commentPhotos, commentStar, commentDate', null, null, 'commentDate desc', `${Math.abs(index)},${Math.abs(count)}`)
		return comment || []
	}

	_checkStoreId(storeId) {
		return /^[a-zA-Z0-9]{10}$/.test(storeId)
	}

	_checkCodeId(codeId) {
		return /^[a-zA-Z0-9]{32}$/.test(codeId)
	}

	_checkStoreBusiness(timeStr) {
		if(!/^\d{2}:\d{2}-\d{2}:\d{2}$/.test(timeStr))
			return this.reject('-2015')
		const [startTime, endTime] = timeStr.split('-')
		const [startHours, startMin] = startTime.split(':')
		const [endHours, endMin] = endTime.split(':')
		const date = new Date()
		const nowHours = date.getHours()
		const nowMin = date.getMinutes()
		if((nowHours > startHours || (nowHours == startHours && nowMin >= startMin)) && (nowHours < endHours || (nowHours == endHours && nowMin <= endMin)))
			return true
		else
			return false
	}

	_checkGoodsCategory(categoryId) {
		return /^[a-zA-Z0-9]{4}$/.test(categoryId)
	}

}

module.exports = h_store